﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom;
using Ebc.DesignerDsl.TypeBrowserIntegration;

namespace Ebc.DesignerDsl.TypeBrowserIntegration
{
    public class TypeDefinition
    {
        private UsingsParser parser = new UsingsParser();
        private Action<string> csharpSetter;
        private Action<string> clrSetter;
        private Action<string> addUsing;
        private Func<string> csharpGetter;

        public TypeDefinition(Action<string> csharpSetter, Action<string> clrSetter, Func<string> csharpGetter, Action<string> addUsing)
        {
            this.clrSetter = clrSetter;
            this.csharpSetter = csharpSetter;
            this.csharpGetter = csharpGetter;
            this.addUsing = addUsing;
        }

        public void SetType(Type type)
        {
            if (type != null)
            {
                this.clrSetter(type.AssemblyQualifiedName);
                var parsed = this.parser.ParseToCSharpString(type);
                
                foreach (var item in parsed.Usings)
                {
                    this.addUsing(item);
                }

                this.csharpSetter(parsed.CSharpType);
            }
            else
            {
                this.clrSetter(string.Empty);
                this.csharpSetter(string.Empty);
            }
        }

        public override string ToString()
        {
            return this.csharpGetter();
        }
    }
}